package com.app.controller;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import com.app.view.DialogHandler;

@SuppressWarnings("javadoc")
public class FileManager implements Serializable{
	
	private MovieList movies;
	private DialogHandler dialog;
	
	public FileManager(){
		this.dialog = new DialogHandler();
	}
	
	public void loadFromFile(){
		try{
			FileInputStream file = new FileInputStream("./data/movieList.obj");
			ObjectInputStream inStream = new ObjectInputStream(file);
			
			this.movies = (MovieList) inStream.readObject();
		}
		catch(FileNotFoundException e){
			this.movies = new MovieList();
		}
		catch(IOException e){
			this.dialog.exception("loadFromFile(): IOException");
		}
		catch(ClassNotFoundException e){
			this.dialog.exception("loadFromFile():ClassNotFoundException");
		}
	}
	
	public void saveToFile(){
		try{
			FileOutputStream file = new FileOutputStream("./data/movieList.obj");
			ObjectOutputStream outStream = new ObjectOutputStream(file);
			outStream.writeObject(this.movies);
		}
		catch(FileNotFoundException FNFE){
			this.dialog.exception("saveToFile(): FileNotFoundException");
		}
		catch(IOException IOE){
			this.dialog.exception("saveToFile(): IOException");
		}
	}
	
	public MovieList getList(){return this.movies;}
}
